package cn.softcits.thread.locker;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author Rocket
 * @version 10.0.1
 * @date 2021-07-12 11:24 AM
 */
public class MyService {
    private Lock lock = new ReentrantLock();
    private ReadWriteLock rw = new ReentrantReadWriteLock();

    public void testMethod() {
        //lock.lock();
        rw.writeLock().lock();
        for(int i=0;i<5;i++) {
            System.out.println("ThreadName= "+Thread.currentThread().getName()+(" "+(i+1)));
        }
        //lock.unlock();
        rw.writeLock().unlock();
    }

    static class MyThread extends Thread {
        private MyService myService;

        public MyThread(MyService myService) {
            this.myService = myService;
        }

        @Override
        public void run() {
            myService.testMethod();
        }
    }

    public static void main(String[] args) {
        MyService myService = new MyService();
        MyThread t1 = new MyThread(myService);
        MyThread t2 = new MyThread(myService);
        t1.start();
        t2.start();
    }

}
